#encoding:utf-8
import os , sys 
try:
	from py_ex import *
except:
	import logging

_quality = "80"
_speed = "1"

def chk_quant_env():
	try:
		PNGQUANT_HOME = os.environ['PNGQUANT_HOME']
	except Exception:
		logging.error ("PNGQUANT_HOME not defined. Please define PNGQUANT_HOME in your environment")
		return -1
	return PNGQUANT_HOME



def __get__alltextures(dir):
	for filename in os.listdir(dir):
		obj = os.path.join(dir , filename)
		
		if os.path.isfile(obj):
			if obj.find(".png") != -1:
				allpng.insert(len(allpng) + 1 , obj)
		else:
			__get__alltextures(obj)

allpng = []
def quant(tardir = os.getcwd()):
	#Get dir all textures
	__get__alltextures(tardir)	

	#Chk Enviorment
	chk_quant_env()

	#Quant console
	for texture in allpng:
		logging.info("Pngquant the texture %s by [quality(%s) $$ speed(%s)]" , (texture , _quality , _speed))
		os.system("pngquant -f --speed " + _speed + " --quality " + _quality + " --output " + texture + " --force" + " " + texture)
	